package com.itboye.jinpuyiyuan;

import android.content.Intent;
import android.graphics.Color;
import android.os.Build;
import android.os.Bundle;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.Animation;
import android.view.animation.TranslateAnimation;
import android.widget.ImageView;
import android.widget.TextView;

import com.itboye.jinpuyiyuan.activity.GuideActivity1;
import com.itboye.jinpuyiyuan.activity.login.LoginOrRegisterActivity;
import com.itboye.jinpuyiyuan.base.BaseActivity;
import com.itboye.jinpuyiyuan.utils.sputil.Const;
import com.itboye.jinpuyiyuan.utils.sputil.SPUtils;

import java.util.ArrayList;
import java.util.Timer;
import java.util.TimerTask;

/**
 * Created by Administrator on 2017/12/14 0014.
 */

public class GuideActivity extends BaseActivity {
    private ViewPager guideVp;
    private TextView testTv;
    private ArrayList<ImageView> list;
    private MyPagerAdapter myPagerAdapter;

    private Timer timer;
    private ImageView sliderImg,guideImg_01,guideImg_02 ;//滑块
    private boolean isFirstDownload;
    private int currIndex;//当前页
    private int width;//vp 滑动一页的距离
    private int beforePosition=0;//上一次的位置
    private int [] imgs={R.drawable.yindao3x_01,R.drawable.yindao3x_02,R.drawable.yindao3x_03};
    private ImageView splashImg;
    @Override
    protected void onCreate(Bundle arg0) {
        super.onCreate(arg0);
        setContentView(R.layout.activity_guide);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            getWindow().setStatusBarColor(Color.TRANSPARENT);
        }
        initViewData();


    }

    private void initViewData() {

        list=new ArrayList<>();
        timer=new Timer();
        currIndex= 0;
        isFirstDownload= (boolean) SPUtils.get(getApplication(),null,"first_download",true);
        for (int i=0;i<3;i++){
        ImageView imageView = new ImageView(this);
        imageView.setImageDrawable(getResources().getDrawable(imgs[i]));
        list.add(imageView);
        }
        myPagerAdapter=new MyPagerAdapter(list);
        guideVp.setAdapter(myPagerAdapter);

        timer.schedule(new TimerTask() {
            @Override
            public void run() {
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        splashImg.setVisibility(View.GONE);
                        if (!isFirstDownload){
                            guideVp.setVisibility(View.GONE);
                            startActivity(new Intent(GuideActivity.this, MainActivity.class));
                            GuideActivity.this.finish();


                        }

                    }
                });

            }
        },3000);
        guideVp.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

                if (position==currIndex){// 往右滑的时候position的值为当前的项的值
                    tranSlate(beforePosition,10,currIndex*width+width*positionOffset);
                    beforePosition = (int) (currIndex * width + width * positionOffset);

                }else if (position+1==currIndex){ // 往左滑的时候arg0的值为前一项的值
                    tranSlate(beforePosition,10,position*width+width*positionOffset);
                    beforePosition = (int) (position * width + width * positionOffset);
                }


            }

            @Override
            public void onPageSelected(int position) {
             if (position==(list.size()-1)){
                 testTv.setVisibility(View.VISIBLE);
             }else {
                 testTv.setVisibility(View.GONE);
             }
             currIndex=position;
            }

            @Override
            public void onPageScrollStateChanged(int state) {


            }
        });
    }

    @Override
    public void onClick(View view) {
        super.onClick(view);
        switch (view.getId()){
            case R.id.testTv:
//                SPUtils.put(getApplication(),null,"isFirstOpen",true);

                startActivity(new Intent(GuideActivity.this, LoginOrRegisterActivity.class));
                GuideActivity.this.finish();
                SPUtils.put(getApplication(),null,"first_download",false);
//              startActivity(new Intent(this,MainActivity.class));

            this.finish();
                break;
        }
    }

    class MyPagerAdapter extends PagerAdapter{

            private ArrayList<ImageView> list;
            public MyPagerAdapter(ArrayList<ImageView> list){
                this.list=list;
            }
            @Override
            public int getCount() {
                return this.list.size();
            }

        @Override
        public int getItemPosition(Object object) {
            return super.getItemPosition(object);
        }

        @Override
            public boolean isViewFromObject(View view, Object object) {
                return view==object;
            }

        @Override
        public Object instantiateItem(ViewGroup container, int position) {
                container.addView(list.get(position));
            return list.get(position);
        }

        @Override
        public void destroyItem(ViewGroup container, int position, Object object) {
            container.removeView(list.get(position));
        }
    }

    /**
     * 平移动画
     * @param beforePosition 之前的位置
     * @param durationMillis time
     * @param distance       需要滑动的距离
     */
    public void tranSlate(int beforePosition, long durationMillis,float distance) {
        Log.v("offest", "" + distance);
        Animation animation = new TranslateAnimation(beforePosition,
                distance, 0, 0);// 平移动画
        Log.v("currentoffest", "" + currIndex * distance);
        Log.v("currentItem", "" + distance);
        animation.setFillAfter(true);// 动画终止时停留在最后一帧，不然会回到没有执行前的状态
        animation.setDuration(durationMillis);// 动画持续时间0.2秒
        sliderImg.startAnimation(animation);// 是用ImageView来显示动画的
    }

    @Override
    public void onWindowFocusChanged(boolean hasFocus) {
        super.onWindowFocusChanged(hasFocus);
        int left = guideImg_01.getLeft();
        int left1 = guideImg_02.getLeft();
        //滑动的距离
        width=left1-left;
    }
}
